package com.study.spmongodb.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.joda.time.DateTime;

import java.util.Date;

/**
 * @author MI
 * @version 1.0
 * @date 2020/10/14 15:15
 */
public class CreateJwt {
    public static void main(String[] args) {
        Date loginDate = new Date();
        final JwtBuilder builder = Jwts.builder()
                .setId("51")
                .setSubject("小马")
                .setIssuedAt(loginDate)
                .signWith(SignatureAlgorithm.HS256, "zhuzi")
                .setExpiration(new DateTime(loginDate).plusMinutes(60).toDate())
                .claim("key", "value")//添加自定义 属性
                ;
        final String compact = builder.compact();
        System.out.println(compact);
        final Claims zhuzi = Jwts.parser().setSigningKey("zhuzi").parseClaimsJws(compact).getBody();
        System.out.println(zhuzi);
        System.out.println("过期时间" + zhuzi.getExpiration());

    }
}
